package nettypro.netty.groupchat;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.aspectj.apache.bcel.generic.NEW;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName GroupChatServerHandler
 * @Description TODO
 * @Author zeny
 * @Date 2020/3/24 0024 21:26
 */
public class GroupChatServerHandler extends SimpleChannelInboundHandler<String> {

    //定义一个channel组，管理所有的channel,GlobalEventExecutor.INSTANCE 是全局事件执行器，单例
    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * @Description 连接时首先执行,保存每个channel,并给所有channel提示
     * @Date 2020/3/24 0024 21:32
     * @param ctx
     * @return void
     **/
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        //给每个channel发送消息
        channelGroup.writeAndFlush("[客户端]" + channel.remoteAddress() + "在 " + simpleDateFormat.format(new Date()) + " 加入聊天\n");
        channelGroup.add(channel);
    }

    /**
     * @Description channel处于活动状态,提示xxx上线
     * @Date 2020/3/24 0024 21:38
     * @param ctx
     * @return void
     **/
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.channel().remoteAddress() + "上线了......");
    }

    /**
     * @Description channel离线
     * @Date 2020/3/24 0024 21:40
     * @param ctx
     * @return void
     **/
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.channel().remoteAddress() + "离线了......");
    }

    /**
     * @Description 断开连接，并提示给在线用户
     * @Date 2020/3/24 0024 21:41
     * @param ctx
     * @return void
     **/
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        //自动移除当前channel
        channelGroup.writeAndFlush("[客户端]" + ctx.channel().remoteAddress() + " 离开了......\n");
    }

    /**
     * @Description 处理数据
     * @Date 2020/3/24 0024 21:43
     * @param ctx
     * @param msg
     * @return void
     **/
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.forEach(ch -> {
            if (ch != channel) {
                //不是当前channel,转发
                ch.writeAndFlush("[客户]" + channel.remoteAddress() + " 说: " + msg + "\n");
            }else {
                ch.writeAndFlush("[自己] 说: " + msg + "\n");
            }
        });
    }

    /**
     * @Description 处理异常
     * @Date 2020/3/24 0024 21:48
     * @param ctx
     * @param cause
     * @return void
     **/
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
